
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using TrainingsApp.Model;
using TrainingsApp.Helper;
using WisDragon.Framework.Data.DataAccess;

namespace TrainingsApp.DAL {
    public class TS_User_Teacher_DAL {
        public static TS_User_Teacher_Collecion TS_User_Teacher_ListByPaging(int pageSize, int pageIndex) {
            TS_User_Teacher_Collecion list = new TS_User_Teacher_Collecion();
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_User_Teacher_ListByPaging");
                cmd.SetParameterValue("@pageSize", pageSize);
                cmd.SetParameterValue("@pageIndex", pageIndex);
                List<TS_User_Teacher_Entity> pageList = cmd.ExecuteEntityList<TS_User_Teacher_Entity>();
                list.TS_User_Teacher_List = pageList;
                list.TotalCount = Convert.ToInt32(cmd.GetParameterValue("@recordCount"));
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return list;

        }


        public static TS_User_Teacher_Collecion TS_User_Teacher_ListToGridByPaging(int pageSize, int pageIndex, string where) {
            TS_User_Teacher_Collecion list = new TS_User_Teacher_Collecion();
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_User_Teacher_ListToGridByPaging");
                cmd.SetParameterValue("@pageSize", pageSize);
                cmd.SetParameterValue("@pageIndex", pageIndex);
                cmd.SetParameterValue("@where", where);
                List<TS_User_Teacher_Entity> pageList = cmd.ExecuteEntityList<TS_User_Teacher_Entity>();
                list.TS_User_Teacher_List = pageList;
                list.TotalCount = Convert.ToInt32(cmd.GetParameterValue("@recordCount"));
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return list;

        }



        public static TS_User_Teacher_Entity TS_User_Teacher_View(System.Int64 UserId, System.Int64 SchoolId, System.Int64 ClassesId) {
            TS_User_Teacher_Entity c = default(TS_User_Teacher_Entity);
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_User_Teacher_View");
                cmd.SetParameterValue("@UserId", UserId);
                cmd.SetParameterValue("@SchoolId", SchoolId);
                cmd.SetParameterValue("@ClassesId", ClassesId);
                c = cmd.ExecuteEntity<TS_User_Teacher_Entity>();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }
            return c;
        }

        public static List<TS_User_Teacher_Entity> TS_User_Teacher_View(System.Int64 UserId) {

            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_User_Teacher_View");
                cmd.SetParameterValue("@UserId", UserId);
                return cmd.ExecuteEntityList<TS_User_Teacher_Entity>();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }
            return new List<TS_User_Teacher_Entity>(0);
        }

        public static List<TS_User_Teacher_Entity> TS_User_Teacher_ViewAllClass(System.Int64 UserId) {

            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_User_Teacher_ViewAllClass");
                cmd.SetParameterValue("@UserId", UserId);
                return cmd.ExecuteEntityList<TS_User_Teacher_Entity>();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }
            return new List<TS_User_Teacher_Entity>(0);
        }




        public static TS_User_Teacher_Entity TS_User_Teacher_Add(List<TS_User_Teacher_Entity> entities) {
            TS_User_Teacher_Entity entity = entities.FirstOrDefault();
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_User_Teacher_Add");
                cmd.SetParameterValue("@UserName", entity.UserName);
                cmd.SetParameterValue("@Password", entity.Password);
                cmd.SetParameterValue("@Name", entity.Name);
                cmd.SetParameterValue("@Gender", entity.Gender);
                cmd.SetParameterValue("@BirthDay", entity.BirthDay);
                cmd.SetParameterValue("@IdentityCard", entity.IdentityCard);
                cmd.SetParameterValue("@Mobile", entity.Mobile);
                cmd.SetParameterValue("@Email", entity.Email);
                cmd.SetParameterValue("@PhotoImage", entity.PhotoImage);
                cmd.SetParameterValue("@UserType", entity.UserType);
                cmd.SetParameterValue("@UserId", entity.UserId);
                cmd.SetParameterValue("@SchoolId", entity.SchoolId);
                cmd.SetParameterValue("@ClassesId", entity.ClassesId);
                cmd.SetParameterValue("@ManagerStatus", entity.ManagerStatus);
                cmd.SetParameterValue("@PositionId", entity.PositionId);
                cmd.SetParameterValue("@CourseId", entity.CourseId);
                cmd.SetParameterValue("@MaterialsId", entity.MaterialsId);
                cmd.SetParameterValue("@TitleId", entity.TitleId);
                cmd.SetParameterValue("@UserSource", entity.UserSource);
                cmd.ExecuteNonQuery();
                entity.UserId = Convert.ToInt64(cmd.GetParameterValue("UserId"));

                for (int i = 1; i < entities.Count; i++) {
                    TS_User_Teacher_Add(entity.UserId.Value, entities[i]);
                }
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return entity;
        }


        public static TS_User_Teacher_Entity TS_User_Teacher_Add(long userId, TS_User_Teacher_Entity entity) {
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_User_Teacher_AddOne");
                cmd.SetParameterValue("@UserId", userId);
                cmd.SetParameterValue("@SchoolId", entity.SchoolId);
                cmd.SetParameterValue("@ClassesId", entity.ClassesId);
                cmd.SetParameterValue("@ManagerStatus", entity.ManagerStatus);
                cmd.SetParameterValue("@PositionId", entity.PositionId);
                cmd.SetParameterValue("@CourseId", entity.CourseId);
                cmd.SetParameterValue("@MaterialsId", entity.MaterialsId);
                cmd.SetParameterValue("@TitleId", entity.TitleId);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return entity;
        }

        public static void TS_User_Teacher_Update(List<TS_User_Teacher_Entity> entities) {
            TS_User_Teacher_Entity entity = entities.FirstOrDefault();
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_User_Teacher_Update");

                //cmd.SetParameterValue("@UserName", entity.UserName);
                cmd.SetParameterValue("@Password", entity.Password);
                cmd.SetParameterValue("@Name", entity.Name);
                cmd.SetParameterValue("@Gender", entity.Gender);
                cmd.SetParameterValue("@BirthDay", entity.BirthDay);
                cmd.SetParameterValue("@IdentityCard", entity.IdentityCard);
                cmd.SetParameterValue("@Mobile", entity.Mobile);
                cmd.SetParameterValue("@Email", entity.Email);
                cmd.SetParameterValue("@PhotoImage", entity.PhotoImage);
                //cmd.SetParameterValue("@UserType", entity.UserType);

                cmd.SetParameterValue("@UserId", entity.UserId);
                cmd.SetParameterValue("@SchoolId", entity.SchoolId);
                cmd.SetParameterValue("@ClassesId", entity.ClassesId);
                cmd.SetParameterValue("@ManagerStatus", entity.ManagerStatus);
                cmd.SetParameterValue("@PositionId", entity.PositionId);
                cmd.SetParameterValue("@CourseId", entity.CourseId);
                cmd.SetParameterValue("@MaterialsId", entity.MaterialsId);
                cmd.SetParameterValue("@TitleId", entity.TitleId);

                cmd.ExecuteNonQuery();

                for (int i = 1; i < entities.Count; i++) {
                    TS_User_Teacher_Add(entity.UserId.Value, entities[i]);
                }
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }
        }

        public static void TS_User_Teacher_Delete(System.Int64 UserId, System.Int64 SchoolId, System.Int64 ClassesId) {
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_User_Teacher_Delete");
                cmd.SetParameterValue("@UserId", UserId);
                cmd.SetParameterValue("@SchoolId", SchoolId);
                cmd.SetParameterValue("@ClassesId", ClassesId);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }
        }
    }
}
